package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.dao.ProductSpecValueDao;
import com.wing.product.model.entity.ProductProcessMaterial;
import com.wing.product.model.entity.ProductSpecValue;
import com.wing.product.model.request.ProductSpecValueForm;
import com.wing.product.model.response.ProductSpecValueVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 产品类型规格值-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductSpecValueServiceImpl extends ServiceImpl<ProductSpecValueDao, ProductSpecValue> implements ProductSpecValueService {

    @Autowired
    private ProductSpecValueDao productSpecValuedao;

    @Override
    public int add(ProductSpecValueForm productSpecValueForm) {

        //校验

        ProductSpecValue productSpecValue =new ProductSpecValue();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductSpecValue.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productSpecValueForm, productSpecValue, copyOptions);

        return baseMapper.insert(productSpecValue);
    }

    @Override
    public List<ProductSpecValueVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductSpecValueVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductSpecValueVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductSpecValueVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<ProductSpecValueVO> getProductSpecValueByProductSpecValueIds(String[] ids) {
        return productSpecValuedao.getProductSpecValueByProductSpecValueIds(ids);
    }

    @Override
    @Transactional
    public Long updateBatch(List<ProductSpecValueForm> productSpecValueForms) {
        //List<Long> collect = productSpecValueForms.stream().map(ProductSpecValueForm::getId).collect(Collectors.toList());
        //boolean remove = this.remove(new QueryWrapper<ProductSpecValue>().in("product_spec_id", collect));
        //利用json 实现list 拆包解包
        List<ProductSpecValue> productSpecValues = JSON.parseObject(JSON.toJSONString(productSpecValueForms), new TypeReference<List<ProductSpecValue>>() {
        });
        for (ProductSpecValue productSpecValue : productSpecValues) {
            QueryWrapper<ProductSpecValue> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_spec_id",productSpecValue.getProductSpecId());
            queryWrapper.eq("value",productSpecValue.getValue());
            List<ProductSpecValue> list = this.list(queryWrapper);
            if(list!=null&&list.size()>0){
                return productSpecValue.getProductSpecId();
            }
        }
        boolean b = this.saveBatch(productSpecValues);
        if(b){
            return 1L;
        }else {
            return 0L;
        }

    }
}
